﻿Imports System.IO

Public Class frm_DHL
   
    Dim tmpData As New DHLDataStructure
    Private Sub frm_DHL_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Label2.Text = "提示：" & vbCrLf & "  双击列表相应行数进行数据修改！"
        TB_SavePath.Text = Form1.SaveFile.Text
        With ListView1
            .Items.Add("起始号码1") '0
            .Items.Add("起始号码2") '1
            .Items.Add("终止号码1") '2
            .Items.Add("终止号码2") '3
            .Items.Add("REGION") '4
            .Items.Add("COUNTRY") '5
            .Items.Add("REGION COORD") '6
            .Items.Add("PRODUCT") '7
            .Items.Add("PERIOD") '8
            .Items.Add("PO NO") '9
            .Items.Add("TO NO") '10
            .Items.Add("装箱数") '11
            .Items.Add("每盘总箱数") '12
            .Items.Add("起始箱号") '13
            .Items.Add("起始盘号") '14
            SetItemValue(.Items(0), 1, SaveData.DHLData.StartNO)
            SetItemValue(.Items(1), 1, Form1.InsertChar(SaveData.DHLData.StartNO, " "))
            SetItemValue(.Items(2), 1, SaveData.DHLData.EndNo)
            SetItemValue(.Items(3), 1, Form1.InsertChar(SaveData.DHLData.EndNo, " "))
            SetItemValue(.Items(4), 1, SaveData.DHLData.REGION)
            SetItemValue(.Items(5), 1, SaveData.DHLData.Country)
            SetItemValue(.Items(6), 1, SaveData.DHLData.RegionCoord)
            SetItemValue(.Items(7), 1, SaveData.DHLData.Product)
            SetItemValue(.Items(8), 1, SaveData.DHLData.Period)
            SetItemValue(.Items(9), 1, SaveData.DHLData.PO_NO)
            SetItemValue(.Items(10), 1, SaveData.DHLData.TO_NO)
            If SaveData.DHLData.StartXH > 0 Then
                SetItemValue(.Items(13), 1, SaveData.DHLData.StartXH)
            Else
                SetItemValue(.Items(13), 1, "1")
            End If
            If SaveData.DHLData.StartPH > 0 Then
                SetItemValue(.Items(14), 1, SaveData.DHLData.StartPH)
            Else
                SetItemValue(.Items(14), 1, "1")
            End If
            If SaveData.DHLData.每箱号码数 > 0 Then
                SetItemValue(.Items(11), 1, SaveData.DHLData.每箱号码数)
            Else
                SetItemValue(.Items(11), 1, "1000")
            End If
            If SaveData.DHLData.每盘总箱数 > 0 Then
                SetItemValue(.Items(12), 1, SaveData.DHLData.每盘总箱数)
            Else
                SetItemValue(.Items(12), 1, "90")
            End If
        End With
        CommFunction.ChangeButtonFromFrm(Me)
    End Sub

    Private Sub TB_SavePath_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TB_SavePath.KeyPress
        'e.Handled = True
    End Sub

    Private Sub Btn_OpenSaveFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_OpenSaveFile.Click
        Dim NewDialog As New SaveFileDialog
        With NewDialog
            .Title = "保存"
            .Filter = "文本文件(*.txt)|*.txt"
            .FileName = "1"
            .CheckPathExists = True
            If .ShowDialog = Windows.Forms.DialogResult.OK Then
                TB_SavePath.Text = .FileName
            End If
        End With
    End Sub

    Private Sub ListView1_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseDoubleClick
        Dim Command As String = getItemValue(ListView1.SelectedItems(0), 0)
        Dim lItem As ListViewItem = ListView1.SelectedItems(0)
        Dim ReturnValue As String = vbNullString
        Select Case Command
            Case "起始号码1"
                ReturnValue = ShowInput(Me, "输入起始号码", getItemValue(ListView1.SelectedItems(0), 1))
                If Not String.IsNullOrEmpty(ReturnValue) Then
                    SetItemValue(ListView1.Items(1), 1, Form1.InsertChar(ReturnValue, " "))
                End If
            Case ("起始号码2")
            Case ("终止号码1")
                ReturnValue = ShowInput(Me, "输入终止号码", getItemValue(ListView1.SelectedItems(0), 1))
                If Not String.IsNullOrEmpty(ReturnValue) Then
                    SetItemValue(ListView1.Items(3), 1, Form1.InsertChar(ReturnValue, " "))
                End If
            Case ("终止号码2")
            Case ("REGION")
                ReturnValue = ShowInput(Me, "输入REGION", getItemValue(ListView1.SelectedItems(0), 1), False)
            Case ("COUNTRY")
                ReturnValue = ShowInput(Me, "输入COUNTRY", getItemValue(ListView1.SelectedItems(0), 1), False)
            Case ("REGION COORD")
                ReturnValue = ShowInput(Me, "输入REGION COORD", getItemValue(ListView1.SelectedItems(0), 1), False)
            Case ("PRODUCT")
                ReturnValue = ShowInput(Me, "输入PRODUCT", getItemValue(ListView1.SelectedItems(0), 1), False)
            Case ("PERIOD")
                ReturnValue = ShowInput(Me, "输入PERIOD", getItemValue(ListView1.SelectedItems(0), 1), False)
            Case ("PO NO")
                ReturnValue = ShowInput(Me, "输入PO NO", getItemValue(ListView1.SelectedItems(0), 1), False)
            Case ("TO NO")
                ReturnValue = ShowInput(Me, "输入TO NO", getItemValue(ListView1.SelectedItems(0), 1), False)
            Case ("装箱数")
                ReturnValue = ShowInput(Me, "输入装箱数", getItemValue(ListView1.SelectedItems(0), 1))
            Case ("每盘总箱数")
                ReturnValue = ShowInput(Me, "输入每盘总箱数", getItemValue(ListView1.SelectedItems(0), 1))
            Case ("起始箱号")
                ReturnValue = ShowInput(Me, "输入每盘总箱数", getItemValue(ListView1.SelectedItems(0), 1))
            Case ("起始盘号")
                ReturnValue = ShowInput(Me, "输入每盘总箱数", getItemValue(ListView1.SelectedItems(0), 1))
        End Select
        If Not String.IsNullOrEmpty(ReturnValue) Then SetItemValue(lItem, 1, ReturnValue)
    End Sub

    Private Sub ListView1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged

    End Sub

    Private Sub Btn_CreateData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_CreateData.Click
        Try
            With tmpData
                .StartNO = getItemValue(ListView1.Items(0), 1)
                .EndNo = getItemValue(ListView1.Items(2), 1)
                .REGION = getItemValue(ListView1.Items(4), 1)
                .Country = getItemValue(ListView1.Items(5), 1)
                .RegionCoord = getItemValue(ListView1.Items(6), 1)
                .Product = getItemValue(ListView1.Items(7), 1)
                .Period = getItemValue(ListView1.Items(8), 1)
                .PO_NO = getItemValue(ListView1.Items(9), 1)
                .TO_NO = getItemValue(ListView1.Items(10), 1)
                .每箱号码数 = Val(getItemValue(ListView1.Items(11), 1))
                .每盘总箱数 = Val(getItemValue(ListView1.Items(12), 1))
                .StartXH = Val(getItemValue(ListView1.Items(13), 1))
                .StartPH = Val(getItemValue(ListView1.Items(14), 1))
                If .StartNO >= .EndNo Then MessageBox.Show(Me, "起始号码不能等于中止号码！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information) : Return
            End With
            Dim fs As FileStream
            If File.Exists(TB_SavePath.Text) Then
                If CheckBox1.Checked = False Then
                    If MessageBox.Show(Me, "文件已经存在是否覆盖？", "询问", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.No Then
                        Return
                    Else
                        fs = New FileStream(TB_SavePath.Text, FileMode.Create, FileAccess.Write)
                    End If
                Else
                    fs = New FileStream(TB_SavePath.Text, FileMode.Append, FileAccess.Write)
                End If
            Else
                fs = New FileStream(TB_SavePath.Text, FileMode.Create, FileAccess.Write)
            End If
            Dim fw As New StreamWriter(fs, System.Text.Encoding.Default)
            Dim XS As Long = (tmpData.EndNo - tmpData.StartNO + 1) / tmpData.每箱号码数
            Dim xp As Integer = XS / tmpData.每盘总箱数
            NeroBar1.Maximum = XS
            NeroBar1.Value = 0
            For i = Val(tmpData.StartNO) To Val(tmpData.EndNo) Step tmpData.每箱号码数
                fw.WriteLine(i)
                fw.WriteLine(Form1.InsertChar(i, " "))
                fw.WriteLine(i + tmpData.每箱号码数 - 1)
                fw.WriteLine(Form1.InsertChar(i + tmpData.每箱号码数 - 1, " "))
                fw.WriteLine(FormatByLength(tmpData.StartPH, xp.ToString.Length))
                fw.WriteLine(FormatByLength(tmpData.StartXH, XS.ToString.Length))
                fw.WriteLine(tmpData.每箱号码数.ToString)
                fw.WriteLine(tmpData.REGION)
                fw.WriteLine(tmpData.Country)
                fw.WriteLine(tmpData.RegionCoord)
                fw.WriteLine(tmpData.Product)
                fw.WriteLine(tmpData.Period)
                fw.WriteLine(tmpData.PO_NO)
                tmpData.StartXH += 1
                If (tmpData.StartXH - 1) Mod tmpData.每盘总箱数 = 0 Then tmpData.StartPH += 1
                NeroBar1.Value += 1
                My.Application.DoEvents()
            Next
            SetItemValue(ListView1.Items(13), 1, tmpData.StartXH)
            SetItemValue(ListView1.Items(14), 1, tmpData.StartPH)
            fw.Close()
            fs.Close()
            SaveData.DHLData = tmpData
        Catch ex As Exception
            MessageBox.Show(Me, ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub
End Class